<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * goods short summary.
 *
 * goods description.
 *
 * @version 1.0
 * @author xianbin
 */
class Goods extends Goods_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Goods_model');
        $this->load->library('form_validation');
        $this->load->library('ajaxMsgModel');
    }
    public function goodslist()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;
        $pagesize=10;

        $goods_list=$this->Goods_model->get_page($cupge,$pagesize,"a.*,IFNULL(sum(goods_inventory.inventory_count),0)  as inventory_count","goods","order by goods_id desc","goods_id","where is_del=0","LEFT JOIN goods_inventory on goods_inventory.goods_id=a.goods_id GROUP BY a.goods_id ORDER BY a.goods_id desc");
        $this->load->library('page');
        $p=new Page();
        $data['goodsList']=arrayToObject($goods_list['objlist']);
        $p->init($goods_list['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/goods/goodslist'));
        $data['links']=$p->showPages(1);
        $this->load->view('admin/goods/goodslist.html',$data);

    }

    public function addgoods()
    {
        $this->load->Model('Good_material_model');
        $this->load->Model('Goods_style_model');
        $data['materialList']=$this->Good_material_model->get_allgood_material();
        $data['stylelList']=$this->Goods_style_model->get_allgoods_style();
        $this->load->view('admin/goods/addgoods.html',$data);
    }

    public function add_goods()
    {
        $valiRes= $this->form_validation->run('goods_vali');
        $this->load->library('ajaxMsgModel');
        $goodsModel= new Goods_model();
        if($valiRes)
        {
            $goodsModel->title=$this->input->post('title');
            $goodsModel->imgurl=$this->input->post('imgurl');
            $goodsModel->goods_desc=$this->input->post('goods_desc');
            $goodsModel->shop_price=$this->input->post('shop_price');
            $goodsModel->market_price=$this->input->post('market_price');
            $goodsModel->is_on_sale=$this->input->post('is_on_sale');
            $goodsModel->is_new=$this->input->post('is_new');
            $goodsModel->is_hot=$this->input->post('is_hot');
            $goodsModel->seo_keyword=$this->input->post('seo_keyword');
            $goodsModel->seo_description=$this->input->post('seo_description');
            $goodsModel->weight=$this->input->post('weight');
            $goodsModel->addtime=$this->input->post('addtime');
            $goodsModel->style=$this->input->post('style');
            $goodsModel->good_material_id=$this->input->post('good_material_id');
            $goodsModel->goods_style_id=$this->input->post('goods_style_id');
            $goodsModel->thumbnail_img=$this->input->post('thumbnail_img');
            $goodsModel->is_promotion=$this->input->post('is_promotion');
            $goodsModel->discount=$this->input->post('discount');
            $res= $goodsModel->insert_goods($goodsModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));

    }

    public function editgoods()
    {
        $goodsid=$this->uri->segment(4);
        $this->load->Model('Good_material_model');
        $this->load->Model('Goods_style_model');
        $data['materialList']=$this->Good_material_model->get_allgood_material();
        $data['stylelList']=$this->Goods_style_model->get_allgoods_style();
        $data['goodsModel']=$this->Goods_model->get_goods_by_id($goodsid);
        $this->load->view('admin/goods/editgoods.html',$data);

    }

    //修改商品
    public function edit_goods()
    {
        $valiRes= $this->form_validation->run('goods_vali');
        $this->load->library('ajaxMsgModel');
        $goodsModel= new Goods_model();
        if($valiRes)
        {
            $goodsModel->goods_id=$this->input->post('goods_id');
            $goodsModel->title=$this->input->post('title');
            $goodsModel->imgurl=$this->input->post('imgurl');
            $goodsModel->goods_desc=$this->input->post('goods_desc');
            $goodsModel->shop_price=$this->input->post('shop_price');
            $goodsModel->market_price=$this->input->post('market_price');
            $goodsModel->is_on_sale=$this->input->post('is_on_sale');
            $goodsModel->is_new=$this->input->post('is_new');
            $goodsModel->is_hot=$this->input->post('is_hot');
            $goodsModel->seo_keyword=$this->input->post('seo_keyword');
            $goodsModel->seo_description=$this->input->post('seo_description');
            $goodsModel->weight=$this->input->post('weight');
            $goodsModel->addtime=$this->input->post('addtime');
            $goodsModel->good_material_id=$this->input->post('good_material_id');
            $goodsModel->goods_style_id=$this->input->post('goods_style_id');
            $goodsModel->thumbnail_img=$this->input->post('thumbnail_img');
            $goodsModel->is_promotion=$this->input->post('is_promotion');
            $goodsModel->discount=$this->input->post('discount');
            $res= $goodsModel->edit_goods($goodsModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //删除商品-放进回收站
    public function del_goods()
    {
        $goodsid=$this->uri->segment(4);
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $goodsModel= new Goods_model();
        $res=$goodsModel->del_goods($goodsid);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //批量删除商品-放进回收站

    public function batchdel_goods()
    {
        $goods_id=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $goodsModel=new $this->Goods_model();
        $goods_idArray=explode('|',trim($goods_id,'|'));
        $res=0;
        if(count($goods_idArray)>0)
        {
            $res=$goodsModel->batchdel_goods($goods_idArray);
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败,未选择商品';
            $this->ajaxmsgmodel->Statu='err';
        }
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }



    //尺码管理
    public function editsize()
    {
        $this->load->Model('Size_model');
        $goodsid=$this->uri->segment(4);
        $data['goodssizelist']=$this->Size_model->get_size_goods_size($goodsid);
        $data["goodsid"]=$goodsid;
        $this->load->view('admin/goods/editsize.html',$data);
    }

    public function addgoods_size()
    {
        $this->load->Model('Goods_size_model');
        $this->load->library('ajaxMsgModel');
        $goodssizeModel=new Goods_size_model();
        $goodssizeModel->goods_id=$this->input->post("goodsid");
        $goodssizeModel->size_id=$this->input->post("sizeid");
        $res= $goodssizeModel->insert_goods_size($goodssizeModel);
        if($res)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function delgoods_size()
    {
        $this->load->Model('Goods_size_model');
        $this->load->library('ajaxMsgModel');
        $goodssizeModel=new Goods_size_model();
        $goodssizeModel->goods_id=$this->input->post("goodsid");
        $goodssizeModel->size_id=$this->input->post("sizeid");
        $res= $goodssizeModel->del_goods_size($goodssizeModel);
        if($res)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //颜色管理

    public function editcolor()
    {
        $this->load->Model('Color_model');
        $goodsid=$this->uri->segment(4);
        $data['goodscolorlist']=$this->Color_model->get_color_goods_color($goodsid);
        $data["goodsid"]=$goodsid;
        $this->load->view('admin/goods/editcolor.html',$data);
    }

    public function addgoods_color()
    {
        $this->load->Model('Goods_color_model');
        $this->load->library('ajaxMsgModel');
        $goodscolorModel=new Goods_color_model();
        $goodscolorModel->goods_id=$this->input->post("goodsid");
        $goodscolorModel->color_id=$this->input->post("colorid");
        $res= $goodscolorModel->insert_goods_color($goodscolorModel);
        if($res)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function delgoods_color()
    {
        $this->load->Model('Goods_color_model');
        $this->load->library('ajaxMsgModel');
        $goodscolorModel=new Goods_color_model();
        $goodscolorModel->goods_id=$this->input->post("goodsid");
        $goodscolorModel->color_id=$this->input->post("colorid");
        $res= $goodscolorModel->del_goods_color($goodscolorModel);
        if($res)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }





    //库存管理
    public function editinventory()
    {
        $this->load->Model('Goods_color_model');
        $this->load->Model('Goods_size_model');
        $goodsid=$this->uri->segment(4);
        $data['goodscolorlist']=$this->Goods_color_model->get_all_color_by_goods_color($goodsid);
        $data['goodssizelist']=$this->Goods_size_model->get_all_size_by_goods_size($goodsid);
        $this->load->view('admin/goods/editinventory.html',$data);
    }
    public function edit_inventory()
    {
        $this->load->Model('Goods_inventory_model');
        $goodsinventoryModel=new Goods_inventory_model();
        $goodsinventoryModel->goods_color_id=$this->input->post("color_id");
        $goodsinventoryModel->goods_size_id=$this->input->post("size_id");
        $goodsinventoryModel->goods_id=$this->input->post("goods_id");
        $goodsinventoryModel->inventory_count=$this->input->post("inventory_count");
        $this->load->library('ajaxMsgModel');
        $goodsinventoryModel_select=$goodsinventoryModel->get_inventory($goodsinventoryModel);
        if(!empty($goodsinventoryModel_select) && count($goodsinventoryModel_select)>0)
        {
            //p($goodsinventoryModel_select);
            $goodsinventoryModel->goods_inventory_id=$goodsinventoryModel_select[0]->goods_inventory_id;
            $res=$goodsinventoryModel->update_goods_inventory($goodsinventoryModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else
        {
            $res=$goodsinventoryModel->insert_goods_inventory($goodsinventoryModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }

        echo(json_encode($this->ajaxmsgmodel));
    }
    public function get_inventory($goodsid,$colorid,$sizeid)
    {
          $this->load->Model('Goods_inventory_model');
          $goodsinventoryModel=$this->Goods_inventory_model;
          $goodsinventoryModel->goods_id=$goodsid;
          $goodsinventoryModel->goods_size_id=$sizeid;
          $goodsinventoryModel->goods_color_id=$colorid;
          $val=$this->Goods_inventory_model->get_inventory_val($goodsinventoryModel);
          echo !empty($val)?$val->inventory_count:0;
    }
}